package com.group3.admin.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.group3.admin.listener.ContextListener;
import com.group3.admin.service.IAMerchantService;
import com.group3.admin.service.impl.AMerchantServiceImpl;
import com.group3.entity.AMerchant;


/**
 * Servlet implementation class ReviewServlet
 * 	管理员提交更新审核状态的请求，更新服务器数据
 */
//@WebServlet("/ReviewServlet")				    
public class ReviewServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	IAMerchantService iAMerchantService = new AMerchantServiceImpl();
	 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReviewServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String mId = request.getParameter("mId");		// 被更新的Merchant
		String type = request.getParameter("type");// 审核状态
		String reason = request.getParameter("reason"); // 审核回复理由
		
		// 因为传过来的不是整个 bean ，所以先从DB拿 
		AMerchant aMerchant = iAMerchantService.queryAMerchantByMId(mId);
		
		if ("ASSESSED".equals(type)) {
			aMerchant.setStatus("WHITE");			
		} else {
			System.out.println("Not assessed ! " + type);
		}
		aMerchant.setType(type);
		aMerchant.setReason(reason);
	
		System.out.println("ReviewServlet : Update AMerchant " + mId);
		System.out.println("ReviewServlet : review status changed to " + type);
		System.out.println("ReviewServlet : Reason changed to" + reason);
				
		String status = "";
		// 判断是否最新 Mlist
		long lastReadMListTime = new Date().getTime();
		if (request.getSession().getAttribute("readMlistTime") != null) {
			lastReadMListTime = ((Date)request.getSession().getAttribute("readMlistTime")).getTime();
		}		
		boolean isNewest =  lastReadMListTime > ContextListener.newestDate.getTime();
		AMerchant am = null;
		if (isNewest) {
			System.out.println("ReviewServlet: is newest");
			// 更新
			 am = iAMerchantService.updateAMerchant(aMerchant);
		} else {
			System.out.println("ReviewServlet: not newest!!!");
		}
		if (am != null && isNewest) {	// Mlist没有变更 ，则更新成功
			status = "success";
			 System.out.println("ReviewServlet : update successfully!");
		} else {	//更新失败
			status = "failed";
			System.out.println("ReviewServlet : update failed!");
		}
		// 转发给MListServlet处理 
//		request.getRequestDispatcher("MListServlet").forward(request, response);
		response.setContentType("application/json;charset=utf-8");
		String jsonString = JSON.toJSONString(status, true);
		response.getWriter().append(jsonString);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
